<?php
/**
 * Created by PhpStorm.
 * User: hhcyc
 * Date: 2018/4/6
 * Time: 15:35
 */

namespace async;


// 需要安装hiredis库; 我安装新版的时候没装这个好像也行
// 异步redis使用，需要编译swoole时候加入-enable-async-redis;  新版本默认开启了

//构成函数可以传入密码['password'=>'zxc36885158']
$redisClient = new \swoole_redis();


// 这种异步回调，相当于保护了现场，直接执行下面的内容，然后再回来恢复现场继续执行
$redisClient->connect('127.0.0.1', 6379, function (\swoole_redis  $redisClient, bool  $result){
    if($result === false){
        echo "connect false".PHP_EOL;
    }else{
        echo "connetc true".PHP_EOL;
    }

    $redisClient->set('key1', 'value2', function (\swoole_redis $redisClient, $result) {
        echo "set".PHP_EOL;
        if ($result === false){ // false, ok
            echo "error:".$redisClient->errMsg.PHP_EOL;
        }
        var_dump($result);

    });

    $redisClient->get('key1', function (\swoole_redis $redisClient, $result) {
        echo "get".PHP_EOL;
        var_dump($result);

    });

    $redisClient->keys('*', function (\swoole_redis $redisClient, $result) {
        echo "keys".PHP_EOL;
        var_dump($result);

    });

    $redisClient->close();
});

echo "start".PHP_EOL;